package com.xxmw.transaction.service.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxmw.transaction.common.entity.P;
import com.xxmw.transaction.mapper.app.BourseApiMapper;
import com.xxmw.transaction.model.app.BourseApi;
import com.xxmw.transaction.model.app.UserBrickApi;
import com.xxmw.transaction.service.app.BourseApiService;
import com.xxmw.transaction.service.app.UserBrickApiService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author luowenliang
 * @since 2020/2/14 16:00
 */
@Service
public class BourseApiServiceImpl extends ServiceImpl<BourseApiMapper, BourseApi> implements BourseApiService {

    @Autowired
    private UserBrickApiService userBrickApiService;

    @Override
    public P<BourseApi> pageList(int pageNum, int pageSize, Integer bourse, String username) {
        QueryWrapper<BourseApi> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("created");
        queryWrapper.eq(null != bourse, "bourse", bourse);
        queryWrapper.eq(StringUtils.isNotBlank(username), "username", username);
        IPage<BourseApi> page = new Page<>(pageNum, pageSize);
        this.page(page, queryWrapper);
        return new P<>(page.getTotal(), page.getRecords());
    }

    @Override
    public List<BourseApi> getUserApi (String username, Integer bourse) {
        QueryWrapper<BourseApi> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(null != bourse, "bourse", bourse);
        queryWrapper.eq("username", username);
        return list(queryWrapper);
    }

    @Override
    public BourseApi getByUserBrick(Long brickId, Integer brouse) {
        LambdaQueryWrapper<UserBrickApi> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserBrickApi::getBourseId, brouse);
        wrapper.eq(UserBrickApi::getUserBrickId, brickId);
        UserBrickApi userBrickApi = userBrickApiService.getOne(wrapper);
        getById(userBrickApi.getId());
        return getById(userBrickApi.getId());
    }

    @Override
    public List<BourseApi> getUserApis(String username) {
        QueryWrapper<BourseApi> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<BourseApi> getBourseByApi(String username, List<Integer> apis) {
        return  baseMapper.getBourseByApi(username, apis);
    }
}
